<template>
  <div class="app-container">

    <span>搜索内容：</span>
    <el-input style="width: 150px; padding: 10px" v-model="keyword" placeholder="请输入内容"></el-input>
    <el-button type="primary" round @click="handleSearch">搜索</el-button>

    <!--    0全部，1学校，2职场，3游戏，4音乐-->
    <el-tabs v-model="type" @tab-click="handleClick">
      <el-tab-pane label="全部" name="0" />
      <el-tab-pane label="学校" name="1" />
      <el-tab-pane label="职场" name="2" />
      <el-tab-pane label="游戏" name="3" />
      <el-tab-pane label="音乐" name="4" />
    </el-tabs>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="110">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="圈子名称" width="150">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="圈子头像" width="200" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.avatar"
            :preview-src-list="[scope.row.avatar]"
          />
        </template>
      </el-table-column>
      <el-table-column label="背景图" width="110" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.bgUrl"
            :preview-src-list="[scope.row.bgUrl]"
          />
        </template>
      </el-table-column>
      <el-table-column label="用户数" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.userCount }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="110" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            circle
            @click="dialogFormVisible = true; circle = scope.row"
          />
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            circle
            @click="handelDelete(scope.row.id)"
          />
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="size"
      :current-page="page"
      :total="total"
      @current-change="fetchData"
    />
    <el-dialog title="编辑话题" :visible.sync="dialogFormVisible">
      <el-form ref="form" :model="circle" label-width="80px">
        <el-form-item label="圈子名称">
          <el-input v-model="circle.name" aria-placeholder="请输入圈子名称" style="width: 150px" />
        </el-form-item>
        <el-form-item label="圈子背景">
          <el-upload
            action="https://admin-api.pengxinmeta.com/admin-api/file/upload"
            list-type="picture-card"
            :on-success="handlePictureCardPreview"
            :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="circle.bgUrl" alt="">
          </el-dialog>
        </el-form-item>

        <el-form-item label="圈子头像">
          <el-upload
            action="https://admin-api.pengxinmeta.com/admin-api/file/upload"
            list-type="picture-card"
            :on-success="handleAvatarPreview"
            :on-remove="handleAvatarRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="circle.avatar" alt="">
          </el-dialog>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, deleteById } from '@/api/circle'
import {update} from "@/api/circle";

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      dialogFormVisible: false,
      list: null,
      dialogVisible: false,
      keyword: '',
      listLoading: true,
      circle: {
        id: '',
        name: '',
        bgUrl: '',
        avatar: ''
      },
      type: '0',
      page: 1,
      size: 10,
      total: 0
    }
  },
  created() {
    this.fetchData(this.page)
  },
  methods: {
    handelDelete(id) {
      this.$confirm('此操作将永久删除该圈子, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById(id).then(res => {
          this.fetchData(this.page)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handlePictureCardPreview(response, file) {
      this.circle.bgUrl = response.data.url
    },
    handleAvatarPreview(response, file) {
      this.circle.avatar = response.data.url
    },
    handleUpdate() {
      console.log(this.dialogImageUrl)
      update({ id: this.circle.id, name: this.circle.name, bgUrl: this.circle.bgUrl, avatar: this.circle.avatar }).then(res => {
        this.dialogFormVisible = false
        this.fetchData(this.page)
        this.$message({
          type: 'success',
          message: '更新成功!'
        })
      })
      this.circle = {}
    },
    handleRemove(file) {
      this.circle.bgUrl = ''
      console.log(file)
    },
    handleAvatarRemove(file) {
      this.circle.avatar = ''
      console.log(file)
    },
    handleClick(tab, event) {
      this.page = 1
      this.fetchData(this.page)
    },
    handleSearch() {
      this.page = 1
      this.fetchData(this.page)
    },
    fetchData(val) {
      this.page = val
      this.listLoading = true
      getList({
        page: this.page,
        size: this.size,
        type: this.type,
        keyword: this.keyword
      }).then(response => {
        this.total = response.total
        console.log(this.page)
        this.list = response.data
        this.listLoading = false
      })
    }
  }
}
</script>
